<template>
	<view class="container">
		<!-- 头部页签 -->
		<view class="header" ref="formRef">
			<my-tab :tabData="tabData" :tabCurrent="tabCurrent" @handleClick="handleTabClick"></my-tab>
		</view>
		<view class="navbar">
			<view class="navbar-item">
				{{ `您正在进行PO：${parentRow.SourceOrderNo}的派工` }}
			</view>
		</view>
		<!-- 数据表格 -->
		<view class="content">
			<view class="table-head">
				<u-table>
					<u-tr>
						<u-th v-for="(col,colIndex) in tableDatas.columns" :key="colIndex+'-'+tabCurrent"
							:width="col.width">
							<u-checkbox v-if="col.prop==='isChecked'" v-model="selectAllData"
								@click.native="selectAll(0)"></u-checkbox>
							<template v-else>{{col.label}}</template>
						</u-th>
					</u-tr>
				</u-table>
			</view>
			<view class="table-body">
				<scroll-view scroll-y="true" style="height: 100%;" @scrolltolower="scrolltolower()"
					:lower-threshold="3">
					<u-table font-size="24">
						<u-tr v-for="(row,rowIndex) in tableDatas.datas" :key="rowIndex+'-'+tabCurrent">
							<u-td v-for="(col,colIndex) in tableDatas.columns"  :key="colIndex" :class="{alignLeft: col.prop=='MaterialName' }" :width="col.width">
								<u-checkbox v-if="col.prop==='isChecked'" v-model="row.isChecked"></u-checkbox>
								<template v-else>{{ row[col.prop] }}</template>
							</u-td>
						</u-tr>
						<u-loadmore :status="tableDatas.loadmoreVisible" @loadmore="loadMore()" color="#3e82f9" />
					</u-table>
				</scroll-view>
			</view>
		</view>

		<view class="btn-part" v-if="hasPermission">
			<template v-for="(item,index) in buttonList">
				<template v-if="index===tabCurrent">
					<u-button v-for="(bItem,bIndex) in item" :type="bItem.type" :disabled="bItem.disabled"
						:key="index+'-'+bIndex" @click="bItem.method">{{bItem.label}}</u-button>
				</template>
			</template>
		</view>
	</view>
</template>

<script>
	/**
	 *  首页点击派工跳转页，选择订单跳转
	 */
	import myTab from '@/components/myTabs/index.vue'
	export default {
		components: {
			myTab
		},
		data() {
			return {
				tabData: ['生产派工', '安装完成'],
				tabCurrent: 0,
				form: {
					page: 1,
					rows: 50,
				},
				tableDatas: {
					dicID: 5633,
					datas: [],
					columns: [{
							prop: 'isChecked',
							label: '选择',
							width: '10%'
						}, {
							prop: 'MaterialName',
							label: '组件',
							width: '45%'
						},
						{
							prop: 'Report86Count',
							label: '未开始',
							width: '15%'
						},
						{
							prop: 'Report87Count',
							label: '进行中',
							width: '15%'
						},
						{
							prop: 'Report88Count',
							label: '已完成',
							width: '15%'
						},
					],
					loading: false,
				},
				buttonList: [
					[{
							label: '进入派工',
							disabled: false,
							type: 'primary',
							method: this.toPage
						},
						{
							label: '安装完成',
							disabled: false,
							type: 'success',
							method: this.schedulingComplete
						},
					],
					[
					// 	{
					// 	label: '进入派工',
					// 	disabled: false,
					// 	type: 'primary',
					// 	method: this.toPage
					// },
					 ]
				],
				parentRow: null,
				selectAllData: false,
				hasPermission: false, // 是否有按钮权限
				// disabled: true, // 按钮是否可用 ，控制已出货和已完工状态订单和组件
			}
		},
		onLoad(options) {
			if (options.row) this.parentRow = JSON.parse(options.row)
			const resulut = this.$store.getters.userInfo.RoleMap?.some(val => val.RoleID === 'E01Admin' || val.RoleID ===
				'R2309260006') // 管理员和车间主管角色
			this.$set(this, 'hasPermission', resulut)
			// 选择父行  packageStatus:25 已完成
			if (this.parentRow.packageStatus === 25) {
				this.buttonList[0][1].disabled = true // 只控制派工完成 按钮不可用
			}
		},
		onShow() {
			this.dataSearch()
		},
		methods: {
			// 获取数据
			async getApsData(remark = 0) {
				const condition = {
					dicID: this.tableDatas.dicID,
					ParamsLabel: this.tabCurrent === 0 ? '未完成' : '已完成',
				}
				Object.assign(condition, this.form)
				condition.OrderNo = this.parentRow.OrderNo
				this.tableDatas.loading = true
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				this.tableDatas.loading = false
				if (!res) return
				const {
					result,
					data,
					count
				} = res
				this.$set(this.tableDatas, 'count', count)
				if (!res.data.length) {
					this.setLoadmore('nomore')
				} else {
					this.tableDatas.datas.push(...res.data)
					this.setLoadmore('loadmore')
				}
			},
			// 查询数据
			dataSearch() {
				this.form.page = 1
				this.tableDatas.datas.length = 0
				this.getApsData()
			},
			// 跳转页面 进入派工
			toPage() {
				const selectData = this.tableDatas.datas.filter(val => val.isChecked)
				if (selectData.length !== 1) return uni.showToast({
					title: '请选择单行数据',
					icon: 'none'
				})
				const row = selectData[0]
				const url = '/pages/Isgo/OrderScheduling/OrderScheduling3'
				const {
					OrderBOMInstallID, // 新增任务需要绑定
					OrderBOMID, // 新增任务需要绑定
					MaterialID, // 新增任务需要绑定
					MaterialName, // 显示组件名称
					OrderID, // 新增任务需要绑定
					SalesOrderDetailID, // 新增任务需要绑定
					ProductionStatus, // 按钮权限控制
				} = row
				const params = {
					...this.parentRow,
					OrderBOMInstallID,
					OrderBOMID,
					MaterialID,
					MaterialName,
					OrderID,
					SalesOrderDetailID,
					packageStatus: ProductionStatus,
				}
				this.$u.route({
					url,
					params: {
						row: JSON.stringify(params)
					}
				})
			},
			// 派工完成
			schedulingComplete() {
				const selectData = this.tableDatas.datas.filter(val => val.isChecked)
				if (!selectData?.length) return uni.showToast({
					title: '请选择数据',
					icon: 'none'
				})
				uni.showModal({
					title: '提示',
					content: `确认指定PO【${selectData.map(val=>val.SourceOrderNo)}】派工完成吗？`,
					success: async (res) => {
						if (res.confirm) {
							// 用户点击确认
							selectData.forEach(val => {
								val.ProductionStatus = 88 // 派工完成状态
							})
							const res = await this.$request.post({
								url: '/APSAPI/SaveData',
								data: selectData
							})
							if (res) {
								const {
									msg
								} = res
								uni.showToast({
									title: msg,
									icon: 'none'
								})
								this.dataSearch()
							}
						}
					}
				})
			},
			// 页签切换
			handleTabClick(index) {
				this.tabCurrent = index
				this.dataSearch()
			},
			// 表格滚动到底部
			scrolltolower() {
				this.loadMore()
			},
			// 加载更多
			loadMore() {
				if (this.tableDatas.loadmoreVisible === 'nomore') return
				this.form.page++
				this.getApsData()
			},
			//配置加载更多
			setLoadmore(status = 'loadmore') {
				const textMenu = {
					loadmore: '滑动或点击加载更多',
					loading: '加载中...',
					nomore: '已无更多数据',
				}
				this.$set(this.tableDatas, 'loadmoreVisible', status)
				this.$set(this.tableDatas, 'loadmoreText', textMenu[status])
			},
			//选择全部
			selectAll() {
				this.tableDatas.datas.forEach(val => {
					this.$set(val, 'isChecked', this.selectAllData)
				})
			}
		},

	}
</script>

<style scoped lang="scss">
	* {
		box-sizing: border-box;
	}

	.container {
		width: 100%;
		height: 100vh;
		overflow: hidden;

		.header {
			height: 80rpx;
			// border-bottom: 1px solid $uni-border-color;
		}

		.navbar {

			height: 50rpx;

			.navbar-item {
				color: $uni-text-color-inverse;
				text-align: center;
				position: fixed;
				// top: 80rpx;
				left: 0;
				height: 50rpx;
				width: 100%;
				line-height: 50rpx;
				z-index: 10;
				background-color: $uni-color-warning;
			}
		}

		.content {
			height: calc(100vh - 226rpx);
			width: 100%;
			// flex: 1;
			display: flex;
			flex-direction: column;
			padding: 10rpx 10rpx 20rpx;
			overflow: hidden;

			.table-head {}

			.table-body {
				flex-grow: 1;
				width: 100%;
				overflow: hidden;

				uni-scroll-view {
					height: 100%;
				}

				.u-table {
					width: 100%;
					overflow-y: auto;
					overflow-x: hidden;
				}
			}

			.u-tr {}

			.u-td {
				height: auto !important;
			}

			.alignLeft{
				text-align: left !important;
			}
			.col-0 {
				// font-size: 18rpx !important;
				// white-space: pre-wrap !important;
			}

			.col-1 {
				// white-space: pre-wrap !important;
				// font-size: 18rpx !important;
				// font-weight: bold;
			}

			.col-4 {
				// color: red !important;
			}

			.col-edit {
				color: $uni-color-primary !important;
				text-decoration-line: underline;
			}

			.col-color-red {
				color: rgb(255, 84, 87) !important;
			}
		}

		.btn-part {
			// display: f;
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 96rpx;
			display: flex;
			align-items: center;
			z-index: 10;
			background-color: $uni-bg-color;
		}
	}
</style>